package com.serverapk.ad;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class PlayerVideoView extends VideoView{
	private Context context;
	private static MediaController mediaController;
	
	public PlayerVideoView(Context context) {
		super(context);
	}
	
	
	public PlayerVideoView(Context context, AttributeSet attrs) {
		super(context,attrs);
		this.context = context;
		// 创建MediaController对象
		mediaController = new MediaController(context);
	}
	
	//播放
	public void palyer(String path){
		this.setVideoPath(path);
		
		//隐藏播放按钮
        mediaController.setVisibility(View.INVISIBLE);
        
	    //VideoView与MediaController建立关联
        this.setMediaController(mediaController);
         
	    //让VideoView获取焦点
        this.requestFocus();
        
	}
	
	//自动循环播放
	public void setAutoPlayer(){
        this.setOnPreparedListener(new OnPreparedListener() {
        	
			public void onPrepared(MediaPlayer mp) {
				// TODO Auto-generated method stub
				mp.start();	
				mp.setLooping(true);	
			}
			
		});
	}
	
	//点击事件
	public void getClick(){
		this.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (isPlaying()){
                    pause();
                }else {
                    start();
                }
				return false;
			}
		    
		});
	}

	/**
	 * 重写该方法，确保播放的视频都是全屏
	 * @param widthMeasureSpec
	 * @param heightMeasureSpec
     */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// 处理
		int width = getDefaultSize(0, widthMeasureSpec);
		int height = getDefaultSize(0, heightMeasureSpec);
		setMeasuredDimension(width, height);
	}
}
